#!/bin/sh

#
# Use to start the On Demand Server (ODS).
#
# NOTE: It should not be necessary to restart the ODS. An entry is
#       placed into /etc/inittab (entry SvcAgentODS) when Service
#       Agent is installed which will automatically restart it. If
#       it stops running the reason for this should be investigated
#       and any problems rectified.

# Ensure that svcagent runs this script to prevent security risks
id=`id`
id=${id#*\(}
id=${id%%\)*}
if [ "$id" != "svcagent" ]
then
  echo "This script must be run by the user svcagent."
  exit 1
fi
export CLASSPATH=$CLASSPATH:/usr/svcagent/lib/ibm.jar:/usr/svcagent/lib/swingall.jar:/usr/svcagent/lib/collections.jar:/usr/svcagent/lib/sahelp.jar:/usr/svcagent/lib/sai18n.jar:/usr/svcagent/lib/salists.jar
# Modified IBM Sai.
# If this script is run with abort option, then, stop the errpt -c -o command
# that is started by the ODS
op=$1
#echo "Printing the number of Arguements..."
#echo $op
noa=$#
#echo $noa
if [ $noa -eq 1 ]
then
  if [ "$op" == "abort" ] || [ "$op" == "-abort" ]
  then
    # Removing the Dialing Processes.. Sajan
    dialer=`ps -ef |grep /usr/svcagent/sbin/SADialer |grep -v grep|awk '{print $2}'`
    if [ -n "$dialer" ]
    then
      ppppid=`ps -ef | egrep "$dialer+.+pppd"|grep -v grep|awk '{print $2}'`
      if [ -n "$ppppid" ]
      then
        kill $ppppid
      fi
      kill $dialer
    fi
    cd /var/svcagent
    if [ -f "locks/lock.ods" ]
    then
      a=locks/lock.ods
      odsid=`cat $a`
      #echo $odsid
      errptid=`ps -ef | egrep "^ *svcagent +.+$odsid+.+/usr/bin/errpt" |grep -v grep|awk '{print $2}'`
      if [ -n "$errptid" ]
      then 
        kill $errptid
      fi 
      if [ -n "$odsid" ]
      then
        kill $odsid
      fi
    else
      echo "ODS is not running"
      exit
    fi
    exit 
  fi
  #else
    #echo "Number of Arguements is not 1..."
fi

cd /var/svcagent
while (`expr 1 > 0`)
do
let count=5
while ( `expr $count > 0` )
do
  [ -d locks ] || mkdir locks
  mkdir locks/olock  2> /dev/null
  if [ $? -eq 0 ]
  then
    if [ -f "locks/lock.ods" ]
    then
      a=locks/lock.ods
      id=`cat $a`
      p=`ps -ef --cols 400| egrep "^ *svcagent +$id .+ com.ibm.ecf.apps.ODS"|grep -v grep|awk '{print $2}' 2>/dev/null`
      #p=`ps wax | egrep "$id .+com.ibm.ecf.apps.ODS"|grep -v grep` 2>/dev/null
      if [ -n "$p" ] 
      then
        rmdir locks/olock
        echo "ODS already running."
        exit
      fi
    fi
    bc=""
    bc=`java -Djava.security.policy=svcagent.policy com.ibm.ecf.apps.Bcp`
    #nohup java $bc -Djava.security.policy=svcagent.policy com.ibm.ecf.apps.ODS >/var/svcagent/logs/log.ods 2>&1 &
    nohup java -Dlog=off $bc -Djava.security.policy=svcagent.policy com.ibm.ecf.apps.ODS >/dev/null 2>&1 &
    PID=$!
    echo $PID > locks/lock.ods
    rmdir locks/olock
    echo "ODS Started."
    #echo "ODS Started. `date`" >> /var/svcagent/logs/log.ods
    #echo "ODS Started. `date`" >> /var/svcagent/logs/stat.ods

    # Note: this script is respawned from /etc/inittab. This means
    # need to keep this process alive so that multiple copies
    # of the ODS are not started.
    wait $PID
    #exit
  else 
    #(( count = count - 1 ))
    let count=`expr $count - 1`
    echo "$0: Waiting for lock permission ... $count"
    sleep 30
  fi
done

if [ $count -eq 0 ]
then
  echo "$0: Lock directory locked"
  rmdir  locks/olock
  let  count=5
 # exit 1
fi

sleep 2
done
